using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class PoolServiceProviderTypeManager
    {

        private PoolServiceProviderTypeManager() { }

        //PoolServiceProviderTypeSearchRequest 1061
        public static void PoolServiceProviderTypeSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            PoolServiceProviderTypes poolSpts = new PoolServiceProviderTypes();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
            String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;

            poolSpts = business.PoolManager.PoolServiceProviderTypeSearch(poolID, serviceProviderTypeID);

            resultMsgType = (int)MessageTypes.PoolServiceProviderTypeSearchResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(poolSpts, typeof(PoolServiceProviderTypes)));
        }


        //PoolServiceProviderTypeInsert 1029
        public static void PoolServiceProviderTypeInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
            String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
            //String status = business.PoolManager.PoolServiceProviderTypeInsert(int.Parse(poolID), int.Parse(serviceProviderTypeID));
            business.PoolManager.PoolServiceProviderTypeInsert(int.Parse(poolID), int.Parse(serviceProviderTypeID));

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //PoolServiceProviderTypesInsert 1063
        public static void PoolServiceProviderTypesInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            PoolServiceProviderTypeLinks poolSptLinks = new PoolServiceProviderTypeLinks();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode poolsptLinksNode = xmlMessage.SelectSingleNode("//pnp:PoolServiceProviderTypeLinks", nsmgr);
            poolSptLinks = (PoolServiceProviderTypeLinks)common.Utilities.DeserializeObject(poolsptLinksNode.OuterXml, typeof(PoolServiceProviderTypeLinks));

            //String status = business.PoolManager.PoolServiceProviderTypeLinkInsert(poolSptLinks);
            business.PoolManager.PoolServiceProviderTypeLinkInsert(poolSptLinks);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //PoolServiceProviderTypeDelete 1028
        public static void PoolServiceProviderTypeDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String poolID = xmlMessage.SelectSingleNode("//pnp:PoolID", nsmgr).InnerText;
            string serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
            //String status = business.PoolManager.PoolServiceProviderTypeDelete(int.Parse(poolID), int.Parse(serviceProviderTypeID));
            business.PoolManager.PoolServiceProviderTypeDelete(int.Parse(poolID), int.Parse(serviceProviderTypeID));

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //PoolServiceProviderTypesDelete 1068
        public static void PoolServiceProviderTypesDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            PoolServiceProviderTypeLinks poolSptLinks = new PoolServiceProviderTypeLinks();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode poolsptLinksNode = xmlMessage.SelectSingleNode("//pnp:PoolServiceProviderTypeLinks", nsmgr);
            poolSptLinks = (PoolServiceProviderTypeLinks)common.Utilities.DeserializeObject(poolsptLinksNode.OuterXml, typeof(PoolServiceProviderTypeLinks));

            //String status = business.PoolManager.PoolServiceProviderTypeLinkDelete(poolSptLinks);
            business.PoolManager.PoolServiceProviderTypeLinkDelete(poolSptLinks);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }
        
    }
}
